<script setup lang="ts">
// 使用 `<script setup>` 语法简化组件的写法

import HelloWorld from "../components/HelloWorld.vue";
import { ref, computed, watchEffect } from "vue";
import { ElCard ,ElButton, ElInput } from "element-plus";
import axios from "axios";
const username = ref("");
const password = ref("");
const globalerror = ref("");

const isUsernameValid = computed(() => {
  return username.value.length >= 3;
});

function GetCookie(name:string) {
    const cookies = document.cookie.split(';');
    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i].trim();
        if (cookie.startsWith(name + '=')) {
            return cookie.substring(name.length + 1);
        }
    }
    return '';
}
const isPasswordValid = computed(() => {
  const regexes = [
    /[a-z]/, // Lowercase letters
    /[A-Z]/, // Uppercase letters
    /[0-9]/, // Numbers
    /[^a-zA-Z0-9]/ // Special characters
  ];

  const matches = regexes.filter(regex => regex.test(password.value));
  return password.value.length >= 6 && matches.length >= 2;
});
watchEffect(() => {
  if (!isUsernameValid.value) {
    console.log("Invalid username");
  }
  if (!isPasswordValid.value) {
    console.log("Invalid password");
  }
});

const login = async () => {

  if (isUsernameValid.value && isPasswordValid.value) {
    let csrftoken = GetCookie('csrftoken');

    console.log("Logging in with:", {
      username: username.value,
      password: password.value,
      csrftoken:csrftoken
    });
    const response = await axios.post("api/v2/login", {
      username: username.value,
      password: password.value,
    }, {
      headers: {
        'X-CSRFToken': csrftoken // replace 'token' with your token value
      }
    });
    if(response.data["state"] == "ok")
    {
      window.location.href = "/";
    }
    else if(response.data["state"] == "error")
    {
      globalerror.value = response.data["error"];
    }
    else
    {
      globalerror.value = "未知错误";
    }
    
  } else {
    console.log("Invalid input");
  }
  
};
</script>

<template>
  <header><HelloWorld msg="欢迎登录Chats Next" /></header>
  <main>  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <Text><span style="font-size: 20pt;">Chats Next</span><br><span style="font-size: 17.5pt;">the most advance next generation of GPTs </span >
        </Text>
      </div>
    </template>
    <div class="login-container">
    <form @submit.prevent="login">
      <div class="form-group">
    <label for="username">账号 : </label>
    <el-input
      size="large"
      type="text"
      id="username"
      v-model="username"
      placeholder="输入你的账号"
      required
    />
    <div v-if="username && !isUsernameValid" class="invalid-font">Invalid username</div>
  </div>
  <div class="form-group">
    <label for="password">密码 : </label>
    <el-input
      size="large"
      type="password"
      id="password"
      v-model="password"
      placeholder="******"
      required
    />
    <div v-if="password && !isPasswordValid" class="invalid-font">Invalid password</div>
  </div>
  <el-button class="login-button" type="success" @click="login" round>登录</el-button>
  <p v-if="globalerror.length>0" class="error">{{ globalerror }}</p>

    </form>
  </div>
  </el-card></main>


</template>

<style scoped>
header {
  line-height: 1.5;
  margin-top: rem;
  max-height: 100vh;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}


/* 样式化登录表单 */
.login-container {
  max-width: 300px;
  margin: 0 auto;
}

form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 10px;
}

label {
  font-weight: bold;
}

input {
  padding: 5px;
  margin-top: 5px;
}

button {
  padding: 10px;
  background-color: #4caf50;
  color: #fff;
  border: none;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-align: center;
}

Text{
  color: #45a049;
}
label{
  color: #45a049;
}
@keyframes flyIn {
  0% {
    transform: translateX(-100%);
    opacity: 0;
  }
  100% {
    transform: translateX(0);
    opacity: 1;
  }
}

.box-card {
  animation: flyIn 1s ease-out;
  width: 300px;
  background-color:#00000010;
  border-width: 0;
  border-radius: 30px;
  display: block;
  margin: 0 auto 2rem;
}
.el-card {
  transition: transform 0.3s ease;

}

.el-card:hover {
  box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
  transform: scale(1.05);
}
.login-button {

  background-color: #0091ff; /* Change to your preferred color */
  color: white;
  border: none;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.login-button:hover {
  background-color: #0056b3; /* Darker shade for hover state */
}

.login-button:active {
  background-color: #003d80; /* Even darker shade for active state */
}


@media (min-width: 1024px) {
  
  header {
    margin-top: 2rem;
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }
  .box-card {
  width: 480px;
  height: 380px;
  background-color:#00000010;
  border-width: 0;
  border-radius: 30px;
  display: block;
  margin: 0 auto 2rem;
}
}
.invalid-font{
  color: red;
  font-size: 10pt;
}
</style>
